package com.tcxr.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tcxr.framework.base.WebBase;
import com.tcxr.framework.system.SysParameters;
import com.tcxr.framework.system.Variables;
import com.tcxr.server.ThemeServer;
import com.tcxr.validator.ThemeViewValidator;
import com.tcxr.vo.LoginView;
import com.tcxr.vo.ThemeView;
@Controller
@RequestMapping("/test") 
public class ThemeWeb extends WebBase  {
	@Autowired 
	private ThemeServer themeServer;
	
	@Autowired 
	private ThemeViewValidator themeViewValidator;
	
	
	
	
	
	
	/**
	 * 提交登陆信息
	 * @return
	 */
	@RequestMapping("/formTheme.do" )
	public String formTheme(
			ModelMap modelMap,
			@Valid @ModelAttribute  ThemeView themeView,BindingResult result 
			)
	{
		this.themeViewValidator.validate(themeView, result);//添加验证  
		if (result.hasErrors()) {
			
			
		String temp=themeView.getThContent();
		 
	 
		temp=super.stringFilter(temp);
	 
		themeView.setThContent(temp);
			
			modelMap.addAttribute("themeView", themeView);// 把user对象返回到页面，
														// 这样不至于表单被清空了
			return "/theme/addTheme";
		}else{
			 System.out.println("标题："+themeView.getThTitle());
			 System.out.println("主体内容："+themeView.getThContent());
			 System.out.println("验证码："+themeView.getVerify());
				
			 themeServer.addTheme(themeView);
			 
			 
		}
		
		
	
	 
//		System.out.println("数量:"+themeView.getImg().size());
//		System.out.println("数量(1):"+themeView.getImg().get(0));
//		System.out.println("数量(2):"+themeView.getImg().get(1));
//		System.out.println("数量(3):"+themeView.getImg().get(2));
//		 
		
		
		
		
		
		return "/theme/publishing";
	}
	 
	
	
	/**
	 * 跳转到发布新内容。
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/addTheme")
	public String toTheme( ModelMap modelMap)
	{
		return "/theme/addTheme";
	}
	
	
	/**
	 * 跳到首页
	 * @return
	 */
	@RequestMapping("/index")
	public String toIndex( ModelMap modelMap)
	{
		int eachPage=Variables.THEME_EACH_PAGE;
		
	List<ThemeView> listThemeViews = themeServer.getIndex(1,eachPage);
	pageAction(modelMap, 1, eachPage, listThemeViews);
	
	 
	//System.out.println(listThemeViews.size());
		return "/theme/index";
	}
	/**
	 * 带有传值的首页 
	 * @return
	 */
	@RequestMapping("/index/{page}")
	public String toIndex( ModelMap modelMap ,
			@ModelAttribute("page") int page 
			
			)
	
	{
		int eachPage=Variables.THEME_EACH_PAGE;
	List<ThemeView> listThemeViews = themeServer.getIndex(page,eachPage);
	
	
	 
	pageAction(modelMap, page, eachPage, listThemeViews);
	
	 
		return "/theme/index";
	}
	/**
	 * 页面翻页数据的提取
	 * @param modelMap
	 * @param page
	 * @param eachPage
	 * @param listThemeViews
	 */
	private void pageAction(ModelMap modelMap, int page, int eachPage,
			List<ThemeView> listThemeViews) {
		int count =themeServer.getCountTheme();
		int countPage=count/eachPage ;
		 
		if(count%eachPage !=0 )
		{
			countPage++;
		}
 
		modelMap.put("page",page);
		modelMap.put("countPage",countPage);
		modelMap.put("listThemeViews",listThemeViews);
	}
 
	
	
	
	
}
